package firewall;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ArchivoLog {

	private String path;
	private String filename;
	
	private File fd = null;
	
	private long nLineasEscritas = 0;
	
	private String formatoFecha = "'['yyyy-MM-dd HH:mm:ss']'";
	
	public ArchivoLog(String path, String filename) {
		this.path = path;
		this.filename = filename;
		
		this.fd = new File(this.path + this.filename);
	}
	
	public void loguear(Paquete paquete) {
		
		Date dateTime = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(formatoFecha);
		
		this.escribir(dateFormat.format(dateTime) + paquete + "\n");
		
	}

	private void escribir(String texto) {
		FileWriter output = null;
		
		try {
			output = new FileWriter(this.getFD(),true);
			output.write(texto);
			this.nLineasEscritas++;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(output != null)
					output.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	private File getFD() {
		return fd;
	}

	public long getnLineasEscritas() {
		return nLineasEscritas;
	}
	
//	public String leerLinea() {
//		
//		FileReader reader = null;
//		String linea = null;
//		
//		try {
//			reader = new FileReader(this.getFD());
//			BufferedReader buffer = new BufferedReader(reader);
//			
//			linea = buffer.readLine();
//			
//			buffer.close();
//			
//		} catch (IOException e) {
//			e.printStackTrace();
//		} finally {
//			try {
//				if(reader != null)
//					reader.close();
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
//		
//		return linea;
//	}
	
}
